package com.gm.commonlib.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.InputFilter;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.gm.commonlib.R;


public class CustomDialog extends Dialog implements
        View.OnClickListener {
    private final static int LEFT_BUTTON_ID = 0;
    private final static int RIGHT_BUTTON_ID = 1;
    private TextView mTitle;
    private TextView mContent;
    private TextView mLeftTV;
    private TextView mRightTV;
    private ImageView mCenterImg;
    private LinearLayout mContentView;
    private OnClickListener mLeftListener;
    private OnClickListener mRightListener;
    private TextView mImageButtomTv;
    private View line1, line2;
    private View btn_splite_line;
    private OnSureListener onSureListener;

    public ImageView closeImage;

    public TextView getRightTV() {
        return mRightTV;
    }

    public TextView getTitleTV() {
        return mTitle;
    }

    public CustomDialog(Context context) {
        this(context, true);
    }
    public CustomDialog(Context context, int style) {
        super(context, style);
        init();
    }
    public CustomDialog(Context context, boolean cancelable) {
        super(context, R.style.alert_dialog);
        init();
        setCancelable(cancelable);
    }

    private void init() {
        setContentView(R.layout.custom_dialog_layout);
        mTitle =  findViewById(R.id.dTitle);
        mContent =  findViewById(R.id.dContent);
        mLeftTV =  findViewById(R.id.leftTv);
        mRightTV =  findViewById(R.id.rightTv);
        mContentView =  findViewById(R.id.dContentView);
        mCenterImg =  findViewById(R.id.dCenterImg);
        mImageButtomTv =  findViewById(R.id.dImgButtomTv);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        btn_splite_line = findViewById(R.id.btn_splite_line);
        closeImage=findViewById(R.id.closeImage);
        mLeftTV.setOnClickListener(this);
        mRightTV.setOnClickListener(this);
        closeImage.setOnClickListener(this);
        setCanceledOnTouchOutside(true);
    }

    public void setTitleTextColor(int color) {
        mTitle.setTextColor(color);
    }

    public void setTitleTextSize(int textSize) {
        mTitle.setTextSize(textSize);
    }

    public CustomDialog setTitleBold(boolean isBold) {
        TextPaint tp = mTitle.getPaint();
        tp.setFakeBoldText(true);
        return this;
    }

    public CustomDialog setContentEnable(boolean isEnable) {
        mContent.setEnabled(isEnable);
        return this;
    }

    public CustomDialog setContentHint(String hintString) {
        mContent.setHint(hintString);
        return this;
    }



    public CustomDialog setTitleGravity(int Gravity) {
        mContent.setGravity(Gravity);
        return this;
    }

    public CustomDialog setContentGravity(int Gravity) {
        mContent.setGravity(Gravity);
        return this;
    }


    public void showCloseImage(){
        closeImage.setVisibility(View.VISIBLE);
    }
    /**
     * 获取输入的类容
     *
     * @return
     */
    public String getInputContent() {
        return mContent.getText().toString();
    }

    /**
     * 设置图标下面的文本
     *
     * @param txt
     */
    public CustomDialog setImageButtomTv(String txt) {
        mImageButtomTv.setText(txt);
        return this;
    }

    public CustomDialog setImageButtomTv(int id) {
        mImageButtomTv.setText(id);
        return this;
    }

    public CustomDialog hiddenTitle() {
        mTitle.setVisibility(View.GONE);
        line1.setVisibility(View.GONE);
        return this;
    }

    public CustomDialog hiddenLine() {
        line1.setVisibility(View.GONE);
        return this;
    }

    /**
     * 自定义提醒内容布局
     *
     * @param view
     */
    public CustomDialog setMessageContentView(View view) {
        mContentView.removeAllViews();
        mContentView.addView(view);
        return this;
    }

    /**
     * 自定义提醒内容布局
     *
     * @param viewId
     */
    public CustomDialog setMessageContentView(int viewId) {
        mContentView.removeAllViews();
        View view = View.inflate(getContext(), viewId, null);
        mContentView.addView(view);
        return this;
    }

    /**
     * 返回自定义的View
     *
     * @return
     */
    public View getContentView() {
        return mContentView.getChildAt(0);
    }

    public CustomDialog hideContentArea() {
        line1.setVisibility(View.GONE);
        mContentView.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置是否显示标题.
     *
     * @param resId
     */
    public CustomDialog setTitleVisibility(int resId) {
        mTitle.setVisibility(resId);
        return this;
    }

    /**
     * 设置提示内容.
     *
     * @param resId
     */
    public CustomDialog setMessage(int resId) {
        if (resId > 0) {
            mContent.setText(resId);
        }
        return this;
    }

    /**
     * 设置提示内容.
     *
     * @param message
     */
    public CustomDialog setMessage(String message) {
        if (!TextUtils.isEmpty(message) && !"null".equals(message)) {
            mContent.setText(message);
        }
        return this;
    }

    public CustomDialog showCenterImg() {
        mCenterImg.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 设置标题.


    /**
     * 设置标题.
     *
     * @param title
     */
    public CustomDialog setTitle(String title) {
        mTitle.setText(title);
        return this;
    }

    public void hiddenCenterMsg() {
        mContent.setVisibility(View.GONE);
    }

    /**
     * 设置左侧按钮文本.
     *
     * @param resId
     */
    public CustomDialog setLeftText(int resId) {
        if (resId > 0) {
            mLeftTV.setText(resId);
        }
        return this;
    }

    public CustomDialog setLeftText(String txt) {
        mLeftTV.setText(txt);
        return this;
    }

    public CustomDialog setCenterImg(int id) {
        mCenterImg.setImageResource(id);
        mCenterImg.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 设置右侧按钮文本.
     *
     * @param resId
     */
    public CustomDialog setRightText(int resId) {
        if (resId > 0) {
            mRightTV.setText(resId);
        }
        return this;
    }

    public CustomDialog setRightText(String content) {
        mRightTV.setText(content);
        return this;
    }

    public CustomDialog setOnRightListener(OnClickListener listener) {
        this.mRightListener = listener;
        return this;
    }

    public CustomDialog setOnLeftListener(OnClickListener listener) {
        this.mLeftListener = listener;
        return this;
    }


    public CustomDialog hideLeftBtn() {
        mLeftTV.setVisibility(View.GONE);
        btn_splite_line.setVisibility(View.GONE);
        return this;
    }

    public CustomDialog hideRightBtn() {
        mRightTV.setVisibility(View.GONE);
        btn_splite_line.setVisibility(View.GONE);
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rightTv && onSureListener != null) {
            String input = getInputContent();
//            if (TextUtils.isEmpty(input)) {
//                mContent.setError(mContent.getHint().toString());
//                mContent.requestFocus();
//                return;
//            }
            onSureListener.onSure(getInputContent());
        }
        dismiss();
        int id = v.getId();
        if (id == R.id.leftTv) {
            if (mLeftListener != null) {
                mLeftListener.onClick(this, LEFT_BUTTON_ID);
            }
        } else if (id == R.id.rightTv) {
            if (mRightListener != null) {
                mRightListener.onClick(this, RIGHT_BUTTON_ID);
            }
        }

    }

    public CustomDialog setLeftButtonTextColor(int color) {
        mLeftTV.setTextColor(color);
        return this;
    }

    public CustomDialog setLeftButtonBgColor(int color) {
        mLeftTV.setBackgroundColor(color);
        return this;

    }

    public CustomDialog setLeftButtonBgResource(int color) {
        mRightTV.setBackgroundResource(color);
        return this;

    }

    public CustomDialog setRightButtonTextColor(int color) {
        mRightTV.setTextColor(color);
        return this;
    }

    public void setRightButtonSize(int size) {
        mRightTV.setTextSize(size);
    }

    public TextView getTitleText() {
        return mTitle;
    }

    public interface OnSureListener {
        void onSure(String string);
    }

    public CustomDialog setOnSureListener(OnSureListener onSureListener) {
        this.onSureListener = onSureListener;
        if (onSureListener != null) {
            mContent.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mContent.requestFocus();
                }
            },1000);
        }
        return this;
    }
}
